package com.zhuiyun.project.api.poetryAuthor.service.Impl;

import cn.hutool.core.bean.BeanUtil;
import com.github.pagehelper.PageHelper;
import com.zhuiyun.project.api.poetryAuthor.entity.PoetryAuthor;
import com.zhuiyun.project.api.poetryAuthor.mapper.PoetryAuthorMapper;
import com.zhuiyun.project.api.poetryAuthor.model.PoetryAuthorModel;
import com.zhuiyun.project.api.poetryAuthor.service.PoetryAuthorService;
import com.zhuiyun.project.common.errorCode.CommonException;
import com.zhuiyun.project.common.errorCode.EmErrorCode;
import com.zhuiyun.project.common.response.CommonResult;
import com.zhuiyun.project.utils.ObjectUtils;
import com.zhuiyun.project.utils.pageUtil.MybatisPageHelper;
import com.zhuiyun.project.utils.pageUtil.PageRequest;
import com.zhuiyun.project.utils.pageUtil.PageResult;
import com.zhuiyun.project.utils.validator.ValidatorUtil;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @Description TODO
 * @Author shy
 * @Date 2023/12/11 8:39
 */
@Service
public class PoetryAuthorServiceImpl implements PoetryAuthorService {

    @Resource
    private PoetryAuthorMapper poetryAuthorMapper;
    /**
     *
     * @Author shy
     * @Date 2023/12/11 13:45 TODO 查询作者
      * @param poetryAuthor
     * @return CommonResult
     */
    @Override
    public CommonResult ListAuthor(PoetryAuthorModel poetryAuthor, PageRequest pageRequest) {
        PageHelper.startPage(pageRequest);
        PoetryAuthor p=BeanUtil.toBean(poetryAuthor, PoetryAuthor.class);
        List<PoetryAuthor> poetryAuthors = poetryAuthorMapper.selectAuthor(p);
        PageResult pageResult= MybatisPageHelper.getPageResult(poetryAuthors);
        if(ObjectUtils.isNotEmpty(pageResult)){
            return CommonResult.ok(pageResult);
        }else {
            return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
        }
    }

    /**
     *
     * @Author shy
     * @Date 2023/12/11 13:45 TODO 根据Id删除作者
      * @param id
     * @return CommonResult
     */
    @Override
    public CommonResult DeleteAuthor(Integer id) {
        if(id!=null&&id!=0){
            Example e=new Example(PoetryAuthor.class);
            e.createCriteria().andEqualTo("authorId",id);
            PoetryAuthor p=new PoetryAuthor();
            p.setDelFlag(1);
            p.setUpdateTime(new Date());
            int i=poetryAuthorMapper.updateByExampleSelective(p,e);
            if(i>0){
                return CommonResult.ok(EmErrorCode.DELETE_OK);
            }else {
                return CommonResult.error(EmErrorCode.DELETE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.ID_NULL);
        }
    }
    /**
     *
     * @Author shy
     * @Date 2023/12/11 13:45   TODO 添加作者
      * @param poetryAuthorModel
     * @return CommonResult
     */
    @Override
    public CommonResult InsertAuthor(PoetryAuthorModel poetryAuthorModel) throws CommonException {
        if(ValidatorUtil.ValidationObject(poetryAuthorModel)){
            PoetryAuthor poetryAuthor= BeanUtil.toBean(poetryAuthorModel, PoetryAuthor.class);
            poetryAuthor.setCreationTime(new Date());
            poetryAuthor.setDelFlag(0);
            int i=poetryAuthorMapper.insert(poetryAuthor);
            if(i>0){
                return CommonResult.ok(EmErrorCode.SAVE_OK);
            }else {
                return CommonResult.error(EmErrorCode.SAVE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.PARAMETER_VALIDATION_ERROR);
        }
    }

    /**
     *
     * @Author shy
     * @Date 2023/12/11 13:46   TODO 修改作者信息
      * @param poetryAuthorModel
     * @return CommonResult
     */
    @Override
    public CommonResult UpdateAuthor(PoetryAuthorModel poetryAuthorModel) throws CommonException {
        if(ValidatorUtil.ValidationObject(poetryAuthorModel)&&poetryAuthorModel.getAuthorId()!=null&&poetryAuthorModel.getAuthorId()!=0){
            Example e=new Example(PoetryAuthor.class);
            e.createCriteria().andEqualTo("authorId",poetryAuthorModel.getAuthorId());
            PoetryAuthor p=BeanUtil.toBean(poetryAuthorModel, PoetryAuthor.class);
            p.setUpdateTime(new Date());
            int i=poetryAuthorMapper.updateByExampleSelective(p,e);
            if(i>0){
                return CommonResult.ok(EmErrorCode.UPDATE_OK);
            }else {
                return CommonResult.error(EmErrorCode.UPDATE_ERROR);
            }
        }else {
            return CommonResult.error(EmErrorCode.PARAMETER_VALIDATION_ERROR);
        }
    }

    /**
     *
     * @Author shy
     * @Date 2023/12/14 10:48 TODO 根据id 查询作者
      * @param id
     * @return CommonResult
     */
    @Override
    public CommonResult getAuthorById(Integer id) throws CommonException {
        if(id!=null&id!=0){
            Example example=new Example(PoetryAuthor.class);
            example.createCriteria().andEqualTo("authorId",id);
            PoetryAuthor poetryAuthor = poetryAuthorMapper.selectOneByExample(example);
            if(ObjectUtils.isNotEmpty(poetryAuthor)){
                return CommonResult.ok(poetryAuthor);
            }else {
                return CommonResult.error(EmErrorCode.ID_NOT_OBJECT);
            }
        }else {
            throw new CommonException(EmErrorCode.ID_NULL);
        }
    }

    /**
     *
     * @Author shy
     * @Date 2023/12/14 10:48 TODO 查询所有作者
      * @param
     * @return CommonResult
     */
    @Override
    public CommonResult GetAllAuthor() {
        List<PoetryAuthor> poetryAuthors = poetryAuthorMapper.GetAllAuthor();
        return CommonResult.ok(poetryAuthors);
    }
}